﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Docsbox.DataMigration
{

    public class Common
    {
        //add group names in docsbox web event receiver
        public static List<string> Groups = new List<string> { "Auditors", "Consultants" };

        public static string StatusLive = "Live";
        public static string StatusReady = "Ready for Audit";
        public static string StatusAmendmentRequired = "Amendment required";
        public static string StatusAmendmentReady = "Amendment Ready for Audit";
        public static string StatusCompleted = "Completed";

        public static string ListAccountStatus = "AccountStatus";

        public static string ListDocuments = "MTF Documents";

        public static string ListAccounts = "Accounts";

        public static string ListOffices = "FinanceBranches";



        //Account content type fields
        public static string FieldMTFNo = "Title";
        public static string FieldFirstName = "FirstName";
        public static string FieldSurname = "Surname";
        public static string FieldFinanceOffice = "FinanceOffice";
        public static string FieldRegNo = "RegistrationNumber";
        public static string FieldAccountComment = "AccountComment";
        public static string FieldContractDate = "ContractDate";
        public static string FieldRevisionDate = "RevisionDate";
        public static string FieldAccountStatus = "AccountStatus";
        public static string FieldAccountOwner = "AccountOwner";
        public static string FieldIsAmendmented = "IsAmendmented";
        public static string FieldAuditComment = "AuditComment";
        public static string FieldAuditor = "Auditor";
        public static string FieldDriverLicenceApproved = "DriverLicenceApproved";
        public static string FieldDriverLicenseComment = "DriverLicenseComment";
        public static string FieldBankStatementApproved = "BankStatementApproved";
        public static string FieldBankStatementComment = "BankStatementComment";
        public static string FieldConfirmationOfAddressApproved = "ConfirmationOfAddressApproved";
        public static string FieldConfirmationOfAddressComment = "ConfirmationOfAddressComment";
        public static string FieldWagesSlipApproved = "WagesSlipApproved";
        public static string FieldWagesSlipComment = "WagesSlipComment";
        public static string FieldVisaResidencyApproved = "VisaResidencyApproved";
        public static string FieldVisaResidencyComment = "VisaResidencyComment";
        public static string FieldCreditCheckApproved = "CreditCheckApproved";
        public static string FieldCreditCheckComment = "CreditCheckComment";
        public static string FieldInsuranceApproved = "InsuranceApproved";
        public static string FieldInsuranceComment = "InsuranceComment";
        public static string FieldRPWApproved = "RPWApproved";
        public static string FieldRPWComment = "RPWComment";
        public static string FieldCDPCoverApproved = "CDPCoverApproved";
        public static string FieldCDPCoverComment = "CDPCoverComment";
        public static string FieldAffordabilityApproved = "AffordabilityApproved";
        public static string FieldAffordabilityComment = "AffordabilityComment";
        public static string FieldDepositRequirementApproved = "DepositRequirementApproved";
        public static string FieldDepositRequirementComment = "DepositRequirementComment";

        //document lookup field
        public static string FieldAccountLookup = "Account";

        //office list field
        public static string FieldOfficeCode = "FinanceBranchCode";

        public static string ConvertFieldToString(object fieldValue, FieldType type)
        {
            if (fieldValue == null)
                return string.Empty;
            if (type == FieldType.Text)
                return fieldValue.ToString();
            if (type == FieldType.Note)
                return fieldValue.ToString();
            if (type == FieldType.DateTime)
                return fieldValue.ToString();
            if (type == FieldType.DateOnly)
                return Convert.ToDateTime(fieldValue).ToString("dd/MM/yyyy");
            if (type == FieldType.Lookup)
                return ConvertLookupFieldToString(fieldValue);
            if (type == FieldType.Boolean)
                return Convert.ToBoolean(fieldValue.ToString()) ? "Yes" : "No";
            if (type == FieldType.User)
            {
                //return username 
                return ConvertLookupFieldToString(fieldValue);
            }
            return string.Empty;
        }

        public static int ConvertFieldToInt(object fieldValue)
        {
            if (fieldValue == null)
                return -1;
            return Convert.ToInt32(fieldValue);
        }

        public static bool ConvertFieldToBoolean(object fieldValue)
        {
            if (fieldValue == null)
                return false;
            return Convert.ToBoolean(fieldValue.ToString());
        }


        public static string ConvertLookupFieldToString(object lookupData)
        {
            string lookup = lookupData.ToString();
            string splitter = ";#";
            int index = lookup.LastIndexOf(splitter, StringComparison.CurrentCultureIgnoreCase);
            return lookup.Substring(index + 2);
        }

        public static int ConvertLookupFieldToId(object lookupData)
        {
            string lookup = lookupData.ToString();
            string splitter = ";#";
            int index = lookup.LastIndexOf(splitter, StringComparison.CurrentCultureIgnoreCase);
            return Convert.ToInt32(lookup.Substring(0, index));
        }
        public static int GetLookupStatus(SPWeb web, string statusName)
        {
            SPList list = web.Lists[Common.ListAccountStatus];
            foreach (SPListItem item in list.Items)
            {
                if (item["Title"].ToString().Equals(statusName))
                {
                    return item.ID;
                }
            }
            return 0;
        }


        public static string GetLookupAccountStatusValue(SPWeb web, int Id)
        {
            SPList list = web.Lists[Common.ListAccountStatus];
            foreach (SPListItem item in list.Items)
            {
                if (item.ID == Id)
                {
                    return item["Title"].ToString();
                }
            }
            return string.Empty;
        }



        public static string GetLookupOfficeCode(SPWeb web, int officeId)
        {
            SPList offices = web.Lists[Common.ListOffices];
            SPListItem office = offices.GetItemById(officeId);
            if (office != null)
                return ConvertFieldToString(office[Common.FieldOfficeCode], FieldType.Text);
            return string.Empty;
        }

        public static string GetSpUserLookupValue(SPUser user)
        {
            return user.ID + ";#" + user.LoginName;
        }



    }

    public enum FieldType
    {
        Text,
        Note,
        DateTime,
        Lookup,
        DateOnly,
        Boolean,
        User
    }
}
